package com.buer.storage.service.impl;

import com.buer.storage.dao.*;
import com.buer.storage.dto.ProdDto;
import com.buer.storage.entity.*;
import com.buer.storage.service.ProdService;
import com.buer.storage.utils.CommonGeneratorUtil;
import com.buer.storage.utils.OssUtil;
import com.buer.storage.utils.StringUtil;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//装备信息管理
@Service
public class ProdServiceImpl implements ProdService {
    @Resource
    private ProdDao prodDao;

    @Autowired
    private WorderdetailsDao worderdetailsDao;

    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockdetailsDao stockdetailsDao;

    @Autowired
    private DorderdetailsDao dorderdetailsDao;

    //增加装备
    @Override
    public int add(ProdDto prodDto) {
        String worderOrderNum= CommonGeneratorUtil.generatorProdCode();
        prodDto.setProdCode(worderOrderNum);
        return prodDao.add(prodDto);
    }

    @Override
    public int save(ProdDto prodDto) {
        return prodDao.add(prodDto);
    }


    //删除装备
    @Override
    public int delete(int prodId) {
        return prodDao.delete(prodId);
    }

    //修改装备
    @Override
    public int update(ProdDto prodDto) {
        return prodDao.update(prodDto);
    }

    //查询装备
    @Override
    public List<ProdDto> select() {
        return prodDao.select();
    }

    //通过名字或者编号查询
    @Override
    public PageInfo<Prod> selectByName(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Prod> list = prodDao.selectByName(map); // 获取数据
        PageInfo<Prod> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    //通过装备分类查询
    @Override
    public List<ProdDto> selectByCategory(int categoryId) {
        List<ProdDto> prod = prodDao.selectByCategory(categoryId);
        return prod;
    }

    //上传图片
    @Override
    public R upload(MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                String fn = file.getOriginalFilename();
                byte[] data = file.getBytes();

                    String url = OssUtil.upload(fn, data);
                    if (StringUtil.isnoEmpty(url)) {
                        String str = url;
                        String jieguo = str.substring(46,str.indexOf("?"));
                        return R.ok("http://storage1.booleans.ltd"+jieguo);
                    }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public PageInfo<ProdDto> findAllProd(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<ProdDto> list = prodDao.findAllProd(map); // 获取数据
        PageInfo<ProdDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public ProdDto getProdByNameAndSpecial(Map<String, Object> map) {
        return prodDao.getProdByNameAndSpecial(map);
    }

    @Override
    public ProdDto getProdByCode(String prodCode) {
        return prodDao.getProdByCode(prodCode);
    }

    @Override
    public void synchronization(ProdDto prodDto) {
        Map worderdetailMap=new HashMap<>();
        worderdetailMap.put("prodCode",prodDto.getProdCode());

        List<WorderdetailsDO> worderdetailsList = worderdetailsDao.getWorderdetailsList(worderdetailMap);
        for (WorderdetailsDO worderdetailsDO:worderdetailsList){
            worderdetailsDO.setProdImg(prodDto.getProdImg());
            worderdetailsDO.setProdSpecial(prodDto.getProdSpecial());
            worderdetailsDO.setProdName(prodDto.getProdName());
            worderdetailsDO.setCategoryTwoId(prodDto.getCategoryTwoId());
            worderdetailsDO.setEmit(prodDto.getEmit());
            worderdetailsDO.setSupplierId(prodDto.getSupplierId());
            worderdetailsDao.update(worderdetailsDO);
        }

        List<Stock> stockList = stockDao.getStockList(worderdetailMap);
        for (Stock stock:stockList){
            stock.setProdImg(prodDto.getProdImg());
            stock.setProdSpecial(prodDto.getProdSpecial());
            stock.setProdName(prodDto.getProdName());
            stock.setCategoryTwoId(prodDto.getCategoryTwoId());
            stock.setEmit(prodDto.getEmit());

            stockDao.update(stock);
        }

        List<StockdetailsDO> stockdetailsList = stockdetailsDao.findStockdetailsList(worderdetailMap);
        for (StockdetailsDO stockdetails:stockdetailsList){
            stockdetails.setProdSpecial(prodDto.getProdSpecial());
            stockdetails.setProdName(prodDto.getProdName());
            stockdetails.setCategoryTwoId(prodDto.getCategoryTwoId());
            stockdetails.setEmit(prodDto.getEmit());
            stockdetailsDao.update(stockdetails);
        }


        List<DorderdetailsDO> dorderdetailsList = dorderdetailsDao.getDorderdetailsList(worderdetailMap);
        for (DorderdetailsDO dorderdetailsDO:dorderdetailsList){
            dorderdetailsDO.setProdSpecial(prodDto.getProdSpecial());
            dorderdetailsDO.setProdName(prodDto.getProdName());
            dorderdetailsDO.setCategoryTwoId(prodDto.getCategoryTwoId());
            dorderdetailsDO.setEmit(prodDto.getEmit());
            dorderdetailsDO.setProdImg(prodDto.getProdImg());
            dorderdetailsDao.update(dorderdetailsDO);
        }
    }

}
